<?php

require_once 'Zend/Validate/Abstract.php';

class Core_Validate_OpenIdExists extends Zend_Validate_Abstract
{
    const EXISTS = 'openIdExists';

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::EXISTS => "Another user already exists with the openId '%value%'!",
    );

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value does exist already as openid
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $value = (string)$value;
        $this->_setValue($value);

        $mdlUser = new Model_User();
        if (!$mdlUser->openIdExists($value)) {
            return true;
        }
        $this->_error(self::EXISTS);
        return false;
    }
}